package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.awt.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

/**
 * 数据报表service
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public Map<String, Object> getBusinessReportData() {

        HashMap<String, Object> resultMap = new HashMap<>();

        //日期处理 使用默认时区和区域设置获取日历
        Calendar today = Calendar.getInstance();

        //将时 分 秒 毫秒清零
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);

        //设置周一为每周第一天
        today.setFirstDayOfWeek(Calendar.MONDAY);

        //设置本周第一天是周一
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);

        //设置本周最后一天是周日
        Calendar weekLastDay = (Calendar)today.clone();
        weekLastDay.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);

        //设置本月第一天是一号
        Calendar monthFirstDay = (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH, 1);

        //设置本月最后一天根据月份
        Calendar monthLastDay = (Calendar)today.clone();
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH,maxDayOfMonth);

        //将日期类型转换为自己自定义的字符串格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy--MM-dd");


        //会员数据memberDate
        //reportDate -> YYYY--MM--dd            //日期
        resultMap.put("reportDate",df.format(today.getTime()));

        //todayNewMember -> number		        // 今日新增会员
        resultMap.put("todayNewMember", memberDao.countByRegTimeRange(today.getTime(), today.getTime()));

        //      thisWeekNewMember -> number		// 本周新增会员
        resultMap.put("thisWeekNewMember", memberDao.countByRegTimeRange(weekFirstDay.getTime(), weekLastDay.getTime()));

        //      thisMonthNewMember -> number	// 本月新增会员
        resultMap.put("thisMonthNewMember", memberDao.countByRegTimeRange(monthFirstDay.getTime(), monthLastDay.getTime()));

        //      totalMember -> number			//  总会员
        resultMap.put("totalMember", memberDao.countByRegTimeBefore(df.format(today.getTime())));


        //预约数据 orderDao
        //      todayOrderNumber -> number		// 今日预约人数
        resultMap.put("todayOrderNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), null));

        //      thisWeekOrderNumber -> number	// 本周预约人数
        resultMap.put("thisWeekOrderNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), today.getTime(), null));

        //      thisMonthOrderNumber -> number	// 本月预约人数
        resultMap.put("thisMonthOrderNumber", orderDao.countByOrderDateRange(monthFirstDay.getTime(), monthLastDay.getTime(), null));

        //      todayVisitsNumber -> number		// 今日到诊人数
        resultMap.put("todayVisitsNumber", orderDao.countByOrderDateRange(today.getTime(), today.getTime(), Order.ORDERSTATUS_YES));

        //      thisWeekVisitsNumber -> number	// 本周到诊人数
        resultMap.put("thisWeekVisitsNumber", orderDao.countByOrderDateRange(weekFirstDay.getTime(), weekLastDay.getTime(), Order.ORDERSTATUS_YES));

        //      thisMonthVisitsNumber -> number	// 本月到诊人数
        resultMap.put("thisMonthVisitsNumber", orderDao.countByOrderDateRange(monthFirstDay.getTime(), monthLastDay.getTime(), Order.ORDERSTATUS_YES));

        //热门套餐
        resultMap.put("hotSetmeal", orderDao.countByHotSetMeal(4));

        return resultMap;
    }
    @Override
    public Map<String, java.util.List<String>> getMemberReport() {
        try {
            ArrayList<String> dateList = new ArrayList<>();
            ArrayList<String> memberList = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR) - 1;
            int month = calendar.get(Calendar.MONTH) + 1;
            for (int j = 0; j < 12; j++) {
                month++;
                if (month == 13) {
                    month = 1;
                    year++;
                }
                Date parse = new SimpleDateFormat("yyyy-MM").parse(year + "-" + month);
                dateList.add(year + "." + month);
                memberList.add(String.valueOf(memberDao.countByDate(parse)));
            }
            Map<String, List<String>> map = new HashMap<>();
            map.put("months", dateList);
            map.put("memberCount", memberList);
            return map;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
